RolesGuard   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 15
dl 0
loc 16
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A canActivate 0 12 2
1
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
2
import { Reflector } from '@nestjs/core';
3
4
@Injectable()
5
export class RolesGuard implements CanActivate {
6
  constructor(private reflector: Reflector) {}
7
8
  public canActivate(context: ExecutionContext): boolean {
9
    const roles = this.reflector.get<string[]>('roles', context.getHandler());
10
11
    if (!roles) {
12
      return true;
13
    }
14
15
    const request = context.switchToHttp().getRequest();
16
    const { user } = request;
17
18
    return roles.includes(user.getRole());
19
  }
20
}
21